package cc.comac.mom.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import cc.comac.mom.api.QualityParamService;
import cc.comac.mom.bean.QualityAttachmentGroup;
import cc.comac.mom.bean.QualityParam;
import cc.comac.mom.bean.base.LimitPageBean;
import cc.comac.mom.datahandlers.entity.QualityAttachmentGroupDataHandler;
import cc.comac.mom.datahandlers.entity.QualityParamDataHandler;
import cc.comac.mom.entity.QualityParamEntity;
import cc.comac.mom.redis.IdFactory;
import cc.comac.mom.util.ResultBean;

@Service
@RestController
@Transactional
public class QualityParamServiceImpl implements QualityParamService {

	@Autowired
	private QualityParamDataHandler qualityParamDataHandler;

	@Autowired
	private QualityAttachmentGroupDataHandler qualityAttachmentGroupDataHandler;

	@Resource
	private IdFactory idFactory;

	@Override
	public ResultBean insert(@RequestBody QualityParam obj) {
		if (obj != null && obj.getInnerData() != null) {
			List<QualityAttachmentGroup> listAttachmentGroup = obj.getListAttachmentGroup();
			if (listAttachmentGroup != null && listAttachmentGroup.size() > 0) {
				long id = idFactory.generateId();
				obj.getInnerData().setAttachmentGroupId(id);
				for (QualityAttachmentGroup item : listAttachmentGroup) {
					item.getInnerData().setGroupId(id);
				}
				ResultBean resultBean2 = qualityAttachmentGroupDataHandler.insertBatch(listAttachmentGroup);
				if (!resultBean2.success) {
					throw new RuntimeException("保存附件组信息失败");
				}
			}
		}
		ResultBean resultBean2 = qualityParamDataHandler.insert(obj);
		if (!resultBean2.success) {
			throw new RuntimeException("保存失败");
		}

		return resultBean2;
	}

	@Override
	public ResultBean update(@RequestBody QualityParam obj) {
		ResultBean resultBean = new ResultBean(true);
		if (obj != null && obj.getInnerData() != null) {
			List<QualityAttachmentGroup> listAttachmentGroup = obj.getListAttachmentGroup();
			if (listAttachmentGroup != null && listAttachmentGroup.size() > 0) {
				long attachmentGroupId = obj.getInnerData().getAttachmentGroupId();
				if (attachmentGroupId == 0) {
					long id = idFactory.generateId();
					obj.getInnerData().setAttachmentGroupId(id);
					for (QualityAttachmentGroup item : listAttachmentGroup) {
						item.getInnerData().setGroupId(id);
					}
				} else {
					for (QualityAttachmentGroup item : listAttachmentGroup) {
						item.getInnerData().setGroupId(attachmentGroupId);
					}
				}
				ResultBean resultBean2 = qualityAttachmentGroupDataHandler.insertBatch(listAttachmentGroup);
				if (!resultBean2.success) {
					throw new RuntimeException("保存附件组信息失败");
				}
			}

		}
		ResultBean resultBean2 = qualityParamDataHandler.update(obj);
		if (!resultBean2.success) {
			throw new RuntimeException("修改失败");
		}
		return resultBean;
	}

	@Override
	public ResultBean deleteBatch(@RequestBody List<QualityParam> list) {
		// TODO Auto-generated method stub
		return qualityParamDataHandler.deleteBatch(list);
	}

	@Override
	public ResultBean selectByCondition(@RequestBody QualityParam obj) {
		ResultBean resultBean = new ResultBean(true);
		LimitPageBean<QualityParamEntity> limitPageBean = null;
		
		if (obj.getSerchAll() == 1) {
			limitPageBean = qualityParamDataHandler.selectAllByCondition(obj);
		} else {
			limitPageBean = qualityParamDataHandler.selectByCondition(obj);
		}
		resultBean.setObject(limitPageBean);
		return resultBean;
	}

}
